<?php

namespace Framework\auth;
use Framework\auth\interfaces\JwtUserInterface;

/**
 * Class JwtAuth
 * @package Framework\auth
 * @author 飞鸟与鱼
 */
class JwtAuth
{

    /**
     * 是否登陆
     * @var bool
     */
    private $is_login = false;

    /**
     * 是否游客登陆
     * @var bool
     */
    private $is_visitor = false;

    /**
     * @var array|mixed 认证数据信息
     */
    private $data;

    /**
     * @var string 凭证
     */
    private $token;

    /**
     * @var \Framework\auth\interfaces\JwtUserInterface
     */
    private $jwtUser;

    /**
     * @var \Framework\auth\JwtToken
     */
    private $jwtToken;

    public function auth($token)
    {
        if ($this->is_login){
            return true;
        }
        $result = $this->jwtToken->get($token);
        if($result->getIdentity() == JwtToken::VISITOR_IDENTITY){
            $this->is_visitor = true;
            $this->data = $result->getData();
        }else if($result->getIdentity() == JwtToken::USER_IDENTITY){
            $this->data = $this->jwtUser->getUserInfo($result->getId());
        }
        $this->token = $token;
        $this->is_login = true;
        return true;
    }

    public function match($action, $arr = [])
    {
        $arr = is_array($arr) ? $arr : explode(',', $arr);
        if (!$arr) {
            return false;
        }
        $arr = array_map('strtolower', $arr);
        if (in_array(strtolower($action), $arr) || in_array('*', $arr)) {
            return true;
        }
        return false;
    }

    /**
     * @return \Framework\auth\interfaces\JwtUserInterface
     */
    public function getJwtUser(): JwtUserInterface
    {
        return $this->jwtUser;
    }

    /**
     * @param \Framework\auth\interfaces\JwtUserInterface $jwtUser
     */
    public function setJwtUser(JwtUserInterface $jwtUser): void
    {
        $this->jwtUser = $jwtUser;
    }

    /**
     * @return \Framework\auth\JwtToken
     */
    public function getJwtToken(): JwtToken
    {
        return $this->jwtToken;
    }

    /**
     * @param \Framework\auth\JwtToken $jwtToken
     */
    public function setJwtToken(JwtToken $jwtToken): void
    {
        $this->jwtToken = $jwtToken;
    }

    /**
     * @return bool
     */
    public function isIsLogin(): bool
    {
        return $this->is_login;
    }

    /**
     * @param bool $is_login
     */
    public function setIsLogin(bool $is_login): void
    {
        $this->is_login = $is_login;
    }

    /**
     * @return bool
     */
    public function isIsVisitor(): bool
    {
        return $this->is_visitor;
    }

    /**
     * @param bool $is_visitor
     */
    public function setIsVisitor(bool $is_visitor): void
    {
        $this->is_visitor = $is_visitor;
    }

    /**
     * @return array|mixed
     */
    public function getData()
    {
        return $this->data;
    }

    /**
     * @param array|mixed $data
     */
    public function setData($data): void
    {
        $this->data = $data;
    }

    /**
     * @return string
     */
    public function getToken(): string
    {
        return $this->token;
    }

    /**
     * @param string $token
     */
    public function setToken(string $token): void
    {
        $this->token = $token;
    }


}